package com.luobu.javaee.controller;

import com.luobu.javaee.domain.Archive;
import com.luobu.javaee.domain.dto.DocumentResDto;
import com.luobu.javaee.domain.dto.NewArchiveDto;
import com.luobu.javaee.domain.dto.SetArchiveDto;
import com.luobu.javaee.service.ArchiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/archive")
public class ArchiveController {
    @Autowired
    ArchiveService archiveService;
    @PostMapping("/newArchive")
    public ResponseEntity<Long> newArchive(@RequestBody NewArchiveDto dto) throws IOException {
        Long res = archiveService.newArchive(dto.getDocuments(), dto.getArchiveName(), dto.getUserId());
        return ResponseEntity.ok(res);
    }

    @PostMapping("/setDocArchive")
    public ResponseEntity<Void> setArchive(@RequestBody SetArchiveDto dto
    ) throws IOException {
        archiveService.setArchiveId(dto.getDocuments(), dto.getArchiveId());
        return ResponseEntity.ok().build();
    }

    @GetMapping("/searchArchive")
    public ResponseEntity<List<DocumentResDto>> searchArchive(Long archiveId) throws IOException {
        List<DocumentResDto> documentResDtoList = archiveService.searchArchive(archiveId);
        return ResponseEntity.ok(documentResDtoList);
    }

    @GetMapping("/getUserArchive")
    public ResponseEntity<List<Archive>> getUserArchive(Long userId) throws IOException {
        List<Archive> list = archiveService.getUserArchiveList(userId);
        return ResponseEntity.ok(list);
    }

    @GetMapping("/deleteDocFromArchive")
    public ResponseEntity<Void> deleteDocFromArchive(Long archiveId, Long docId){
        archiveService.deleteDocFromArchive(archiveId, docId);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/deleteArchive")
    public ResponseEntity<Void> deleteArchive(Long archiveId) throws IOException {
        archiveService.deleteArchive(archiveId);
        return ResponseEntity.ok().build();
    }
}
